﻿var scrip = '';
var iLayerMaxNum = 10000;
var dbId;
var content = "";
var scripNick = "";
var d1;



document.onmouseup = me;
document.onmousemove = ms;
var scripX;
var scripY;
var isShowLogin = false;

App = {};
(function(){
	
	var console;
	App.init  = function(c){
		this.console = c;
	}
	App.lock = function(bFlag)
	{
		var console = App.console;
		if(bFlag)
		{
			var left=document.body.scrollLeft;
			var top=document.body.scrollTop;
			
			console.style.display = "block";
			console.style.zIndex = 20001;
		 	console.style.height = document.body.clientHeight + "px";
			App.isLocked = true;
		}else{
			console.style.display = "none";				
			App.isLocked = false;
		}
	}
	
	function TB_getPageSize(){
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || document.body.clientHeight || (de&&de.clientHeight);
		arrayPageSize = new Array(w,h) 
		return arrayPageSize;
	}
	
})();


function Move(Object, event)
{
	scrip = Object.id;
	if(document.all)
	{
		document.getElementById(scrip).setCapture();
		scripX = event.x - document.getElementById(scrip).style.pixelLeft;
		scripY = event.y - document.getElementById(scrip).style.pixelTop;

	}
	else if(window.captureEvents)
	{
		window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		scripX = event.clientX - parseInt(document.getElementById(scrip).style.left);
		scripY = event.clientY - parseInt(document.getElementById(scrip).style.top) - d1.offsetTop;
	}
	if(scrip != "scrip"+dbId){
		document.getElementById(scrip).style.zIndex = iLayerMaxNum;  
		iLayerMaxNum = iLayerMaxNum + 1;
	}
}

function ms(evt)
{
	if(scrip!=''){
		if(document.all)
		{
			document.getElementById(scrip).style.left = event.x - scripX;
			document.getElementById(scrip).style.top = event.y - scripY;
		}
		else if(window.captureEvents)
		{
		
/*			var iScrollTop = document.body.scrollTop + document.documentElement.scrollTop;
			var iScrollLeft = document.body.scrollLeft + document.documentElement.scrollLeft;
			var s = document.getElementById(scrip);
			s.style.left = evt.clientX + iScrollLeft - d1.offsetLeft + "px";
      		s.style.top  = evt.clientY + iScrollTop - d1.offsetTop + "px";
*/			
			var s = document.getElementById(scrip);
			s.style.left = evt.clientX - scripX + "px";
      		s.style.top  = evt.clientY - scripY - d1.offsetTop + "px";
		}
	 }
}

function me(evt){
	if(scrip!=''){
		if(document.all)
		{
			document.getElementById(scrip).releaseCapture();
			scrip='';
		}
		else if(window.captureEvents){
			window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			scrip='';
		}
	 }
}

//删除函数
function delMessage(n)
{
	var Messageid = 'scrip' + n;
	document.getElementById(Messageid).style.display = 'none';
	if(n == dbId)App.lock(false);
}

//查询函数
function searchmessage(no)
{
	var b = 0 ;
	if(no)
		 b = no;
	else
		b = document.getElementById("scripNo").value;
	var n = parseInt(b,10);
	if(isNaN(b)){
		alert("纸条编号必须为数字");
		return false;
	}else if(n < 1){
		alert("纸条编号必须为正整数");
		return false;
	}
/*		for(var i = 0;i < sArray.length;i++)
	{
	if(n == sArray[i][0]){
			var Messageid = 'scrip' + n;
			document.getElementById(Messageid).style.display = 'block';
			dbrun(n,false,true);
			break;
		}
		
	}
	if(i == sArray.length){*/
		var strMethod = "get";
		var url = document.URL.substring(0,document.URL.lastIndexOf("/")) + "/service.aspx";
		var sParam = '?serviceid=query&no=' + n + "&patch=" + Date.parse(new Date()); 
		startRequest(url+sParam);
//	}
	return false;

}

function afterCall(oResponse)
{	
	var str = oResponse.responseText;
	eval(str);
}

//动态显示纸条
function showmessage(n) {
	if (sArray[n] != undefined){
		dbrun(n);
	}
}

//双击纸条函数
function dbrun(id,bOpen,bCenter)
{
	dbId = id;
	show(true,id,bCenter);
}

function init(){
	var c = document.getElementById("console");
	d1 = document.getElementById("d1");
	App.init(c);
	if(initNO == 0)return;
	if(initNO < 0){
		alert("您搜索的纸条不存在！");
	}else{
		searchmessage(initNO);
		//QQLoveShowRecevier(initNO);
		//QQLoveShowSingle(initNO);
		
		
	}
}

var tmpText;
function show(b,id,bCenter){
	var flashDiv = document.getElementById("messageflash");
	if(b && (b ^ App.isLocked)){
		var showDiv = document.getElementById("scrip"+id);
		showDiv.style.zIndex = 200101;
		
		tmpText = showDiv.firstChild.lastChild.previousSibling.previousSibling.previousSibling.innerHTML ;
		showDiv.firstChild.lastChild.previousSibling.previousSibling.previousSibling.innerHTML = "";
		showDiv.firstChild.lastChild.previousSibling.previousSibling.innerHTML = "<a href=\"add.aspx\">我也要发字条</a>&nbsp;"+showDiv.firstChild.lastChild.previousSibling.previousSibling.innerHTML;
		if(id == initNO || bCenter){	
			showDiv.style.left = 353;
			showDiv.style.top =  150;
		}
		App.lock(true);
	}else{
		if(isShowLogin)
			return ;
		var showDiv = document.getElementById("scrip"+dbId);
		if(showDiv.firstChild.lastChild.previousSibling.previousSibling.firstChild && showDiv.firstChild.lastChild.previousSibling.previousSibling.firstChild.tagName == "A")
			showDiv.firstChild.lastChild.previousSibling.previousSibling.removeChild(showDiv.firstChild.lastChild.previousSibling.previousSibling.firstChild);
		showDiv.firstChild.lastChild.previousSibling.previousSibling.previousSibling.innerHTML = tmpText;
		showDiv.style.zIndex = iLayerMaxNum;	
		iLayerMaxNum = iLayerMaxNum + 1;
		dbId = 0;
		App.lock(false);
	}
	
}

function showSingle()
{
	var j = sArray.length - 1;
	var str = '<div id="scrip'+sArray[j][0]+'" class="Message" style="position:absolute; left:'+sArray[j][2]+'px;top:'+sArray[j][3]+'px;z-index:'+sArray[j][9]+'" onmousedown="Move(this, event)" ondblclick="dbrun('+sArray[j][0]+')"><div class="Style'+sArray[j][1]+'">\
	<div class="Num">字条编号：'+sArray[j][0]+' <img src="images/icon_close.gif" alt="关闭" onclick="delMessage(\''+sArray[j][0]+'\')"></div>\
	<p class="Detail"><img src="'+sArray[j][4]+'"  class="Heart" /><span class="Head">'+sArray[j][5]+'</span><br />'+sArray[j][6]+'</p>\
	<p class="Channel">'+sArray[j][11]+'</p><p class="Sign">'+sArray[j][7]+'</p><p class="Dis" onclick="showDis('+sArray[j][0]+')">评论('+sArray[j][10]+')</p><p class="Date">'+sArray[j][8]+'</p></div></div>';
	document.getElementById("d1").innerHTML +=  str;
}

function Tip(ID,Attr){
	document.getElementById(ID).style.display = Attr;
}

function showLogin(show,id)
{
	if(!id)
		id = "Login";
	if(show){
		isShowLogin = true;
		document.getElementById(id).style.display = "block";
		document.getElementById("logifr").src = "log_ind.jsp?bu=log_ok.jsp?from=bulletin#pay";
		App.lock(true);
	}else{
		isShowLogin = false;
		document.getElementById(id).style.display = "none";
		App.lock(false);
	}
	return false;
}

var xmlHttp;
function createXMLHttpRequest(){
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}
}

function startRequest(url){
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = afterRequest;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
	
function afterRequest(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			eval(xmlHttp.responseText);
		}
	}
}
function toInt(str){
	var ret=0;
	var mid_num;
	for(var i=0;i<str.length;i++){
		mid_num=str.charAt(i);
		if(mid_num<'0' || mid_num>'9')
		{
			return -1;
		}
		ret=ret*10+(mid_num-'0');
	}
	return ret;
}


function QQLoveShowRecevier(array)
{
	if (!document.getElementById("ViewmyMessageWrap"))
	{
		var e = document.createElement("DIV");
		e.id = "ViewmyMessageWrap";
		QQLoveRecevierBody(array,e);
		document.body.appendChild(e);
	}else{
		QQLoveRecevierBody(array,document.getElementById("ViewmyMessageWrap"));
	}
	showLogin(true,"ViewmyMessageWrap");
}

function QQLoveRecevierBody(array,recevierObj)
{
		var htmlStr = "<h2><span onclick='showLogin(false,\"ViewmyMessageWrap\")'>×</span> <em class='t_bold'>"+array[9]+"("+array[11]+")</em> 写给你的爱墙字条：</h2>";
		htmlStr +=  "<div id='ViewmyMessageLeft'>";
		htmlStr += 	"<div id='Message' class='Message'><div id='styleDiv' class='Style"+array[1]+"'>";
		htmlStr += 	"<div class='Num'>字条编号： "+array[0]+"<img src='images/icon_close.gif' alt='关闭' /></div>";
		htmlStr += 	"<p id='Detail' class='Detail'><img src='"+array[4]+"' name='IconImg'  class='Heart' id='IconImg' /><span class='Head' id='Head'>"+array[5]+"：</span><br />";
		htmlStr += 	"<span id='AreaText'>"+array[6]+"</span></p>";
		htmlStr += 	"<p class='Sign' id='Sign'>"+array[7]+"</p>";
		htmlStr += 	"<p class='Date'>"+array[8]+"</p>";
		htmlStr += 	"</div></div></div>";
		htmlStr += 	"<div id='ViewmyMessageRight'><form action='service' method='post' onsubmit='var v = this.c.value.toString(); if(v.length == 0){alert(\"请输入评论内容\");return false;}'><p>给 <em class='t_bold'>"+array[9]+"</em> 的回复：</p><textarea id='c' name='c'></textarea>"
		htmlStr += 	"<p class='AC'><input type='hidden' name='serviceid' value='subdis'/><input type='hidden' name='redirect' value='true'/><input type='hidden' name='scrip' value='"+array[0]+"'/><input type='submit' id='reremarks' value='' /></p></form></div>";
		recevierObj.innerHTML = htmlStr;
}

function QQLoveShowSingle(array)
{
	var n = array[0];
	for(var i = 0;i < sArray.length;i++)
	{
		if(n == sArray[i][0]){
			var Messageid = 'scrip' + n;
			document.getElementById(Messageid).style.display = 'block';
			dbrun(n,false,true);
			break;
		}
	}
	if(i == sArray.length){
		sArray[sArray.length] = array;
		var str = '<div id="scrip'+array[0]+'" class="Message" style="position:absolute; left:'+array[2]+'px;top:'+array[3]+'px;z-index:'+array[9]+'" onmousedown="Move(this, event)" ondblclick="dbrun('+array[0]+')"><div class="Style'+array[1]+'">\
		<div class="Num">字条编号：'+array[0]+' <img src="images/icon_close.gif" alt="关闭" onclick="delMessage(\''+array[0]+'\')"></div>\
		<p class="Detail"><img src="'+array[4]+'"  class="Heart" /><span class="Head">'+array[5]+'</span><br />'+array[6]+'</p>\
		<p class="Channel">'+array[11]+'</p><p class="Sign">'+array[7]+'</p><p class="Dis" onclick="showDis('+array[0]+')">评论('+array[10]+')</p><p class="Date">'+array[8]+'</p></div></div>';
		document.getElementById("d1").innerHTML +=  str;
		dbrun(array[0],false,true);
	}
}
function showDis(id)
{
	window.open("note.jsp?scrip="+id);
}

function QQLoveTipHide()
{
	try
	{
		if (!document.getElementById("QQLoveTipDiv"))
		{
			QQShowTipCreate();
		}
		document.getElementById("QQLoveTipDiv").style.visibility = "hidden";
		document.getElementById("QQLoveTipImg").removeAttribute("src");
	}
	catch(e)
	{
		return false;
	}
	return true;
}
function QQLoveScripShow(id)
{
	try
	{
		var e = arguments[1] || window.event;
		if (!e)
		{
			return false;
		}
		var div = document.getElementById(id);
		var wTipDiv = div.scrollWidth;
		var hTipDiv = div.scrollHeight;
	
		var wBody = document.body.clientWidth;
		var hBody = document.body.clientHeight;
	
		var xPos = typeof(e.clientX)!="undefined" ? e.clientX : (e.pageX-document.documentElement.scrollLeft);
		var yPos = typeof(e.clientY)!="undefined" ? e.clientY : (e.pageY-document.documentElement.scrollTop);
	
		xPos = (xPos+wTipDiv+20<=wBody) ? (xPos+document.documentElement.scrollLeft+20) : (xPos+document.documentElement.scrollLeft-wTipDiv-20);
//		yPos = (yPos+hTipDiv<=hBody) ? (yPos+document.documentElement.scrollTop+10) : (yPos+document.documentElement.scrollTop-10-hTipDiv);
		yPos = (yPos-hTipDiv>=0) ? (yPos-hTipDiv-hTipDiv/2+document.documentElement.scrollTop) : (10);
		
		div.style.left = xPos+"px";
		div.style.top = yPos+"px";
		div.style.display  = "inline";
	}
	catch(e)
	{
		alert(e);
		return false;
	}
	return true;
}

function QQLoveScripHide(id)
{
	try
	{
		if (document.getElementById(id))
		{
	//		document.getElementById(id).style.visibility = "hidden";
			document.getElementById(id).style.display	= "none";
		}
	}
	catch(e)
	{
		return false;
	}
	return true;
}

